Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
79 lines (60 loc) · 1.77 KB

6.3.2 - Table->column.md

File metadata and controls

79 lines (60 loc) · 1.77 KB

Table->column

内存表增加一列

bool Table->column(string $name, int $type, int $size = 0);
  • $name指定字段的名称
  • $type指定字段类型,支持3种类型,Table::TYPE_INT, Table::TYPE_FLOAT, Table::TYPE_STRING
  • $size指定字符串字段的最大长度,单位为字节。字符串类型的字段必须指定$size

类型

  • Table::TYPE_INT默认为4个字节,可以设置1,2,4,8一共4种长度
  • Table::TYPE_STRING设置后,设置的字符串不能超过此长度
  • Table::TYPE_FLOAT会占用8个字节的内存

整型溢出

由于Swoole底层使用有符号整型,如果传入的数值超过最大长度,可能会出现溢出。因此整数类型安全的值范围是:

  • 1byte(int8):-127 ~ 127
  • 2byte(int16):-32767 ~ 32767
  • 4byte(int32):-2147483647 ~ 2147483647
  • 8byte(int64):不会溢出

x86环境需要内存对齐

/*
Linux raspberrypi 4.4.34-v7+ #930 SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux

gcc version 4.9.2 (Raspbian 4.9.2-10)

model name	: ARMv7 Processor rev 4 (v7l)
BogoMIPS	: 76.80
Features	: half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xd03
CPU revision	: 4
*/

//bad
use Swoole\Table;
$table = new \Swoole\Table(8);
$table->column('data',Table::TYPE_STRING, 1);
$table->create();
$table->set(1,[]);
var_dump($table->get(1));
//result: Bus error

//good
use Swoole\Table;
$table = new \Swoole\Table(8);

$table->column('data',Table::TYPE_STRING, 1);
$table->column('pad',Table::TYPE_STRING, 1);
$table->create();

$table->set(1,[]);
var_dump($table->get(1));
/*
result
array(2) {
  ["data"]=>
  string(0) ""
  ["pad"]=>
  string(0) ""
}
*/